// pages/pingjia/pingjia.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    iconn:true,
    star:"star01",
    star01:"star01",
    star02:"star01",
    star03:"star01",
    star04:"star01",
    pingjia:[{},{},{},{},{},{}],
    proData:[],
    pronum:0,
    orderId:0,
    proId:0,
    num:0,
    content:'',
    imgSrc:[],
    i:0,
    photo:[]
    
  },
  deleImg: function (e) {
    var that = this
    var index = e.target.dataset.index;
    var images = that.data.imgSrc;
    images.splice(index, 1);
    that.setData({
      imgSrc: images
    });
  },
  addImg: function () {
    var that = this
    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        if (tempFilePaths) {
          that.setData({
            hiddenImg: false
          })
        }
        console.log(tempFilePaths)
        that.setData({
          imgSrc: tempFilePaths
        });


      }
    })
  },

  upload: function (e) {
    console.log(456)
    var that = this;
    var imgSrc = that.data.imgSrc;
    var i = that.data.i;
    var length = imgSrc.length;
    wx.uploadFile({
      url: app.d.ceshiUrl + '/Api/Order/uploadimg',
      filePath: imgSrc[i],
      name: 'data',
      success: function (res) {
        console.log('uploadImage success, res is:', res);
        that.setData({
          photo: that.data.photo.concat(res.data),
          i: that.data.i + 1,
        })
        if (that.data.i < length) {
          that.upload();
        } else {
          that.save();
        }
      },

      fail: function ({ errMsg }) {
        wx.hideLoading();
        console.log('uploadImage fail, errMsg is', errMsg)
        return false;
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this;
    var proId = options.proId;
    var pronum = options.num;
    var orderId = options.orderId;
    that.setData({
      pronum:pronum,
      orderId: orderId,
      proId: proId
    });
    wx.request({
      url: app.d.ceshiUrl + '/Api/Product/detail_pj',
      method: 'post',
      data: {
        pro_id: proId,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var status = res.data.status;
        console.log(res);
        if (status == 1) {
          var pro = res.data.pro;
          that.setData({
            proData: pro
          });
        } else {
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
        }
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    });
  },
  save:function (){
    console.log(123)
    var that = this;
    var content = that.data.content;
    var num = that.data.num;
    if(!num){
      wx.showToast({
        title: '请输入满意度！',
      });
      return false;
    };
    if (!content) {
      wx.showToast({
        title: '请输入内容！',
      });
      return false;
    }
    var orderId = that.data.orderId;
    var proId = that.data.proId;
    var photo = that.data.photo;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Order/sendContent',
      method: 'post',
      data: {
        uid: app.d.userId,
        pro_id: proId,
        order_id: orderId,
        content: content,
        num: num,
        photo: photo
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var status = res.data.status;
        if (status == 1) {
          wx.hideLoading();
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
          setTimeout(function () {
            wx.navigateBack({

            })
          }, 2000);
        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
        }
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    });
  },
  sendContent:function (e){
    var that = this;
    // wx.showLoading({
    //   title: '提交评价中...',
    // });
    var imgSrc = that.data.imgSrc;
    var length = imgSrc.length;
    if (length > 0){
      that.upload();
    }else{
      that.save();
    }
    
    
  },
  star_click:function(e) {
    if (e.target.id == 0) {
        this.setData({
          star: "star", 
          star01: "star01",
          star02: "star01",
          star03: "star01",
          star04: "star01",
          num:1
        })
    } else if (e.target.id == 1) {
      this.setData({
        star: "star",
        star01:"star",
        star02: "star01",
        star03: "star01",
        star04: "star01",
        num:2
      })
    } else if (e.target.id == 2) {
      this.setData({
        star: "star",
        star01: "star",
        star02: "star",
        star03: "star01",
        star04: "star01",
        num:3
      })
    } else if (e.target.id == 3) {
      this.setData({
        star: "star",
        star01: "star",
        star02: "star",
        star03: "star",
        star04: "star01",
        num:4
      })
    } else if (e.target.id == 4) {
      this.setData({
        star: "star",
        star01: "star",
        star02: "star",
        star03: "star",
        star04: "star",
        num:5
      })
    }
  },

  bindTextAreaBlur:function (e) {
    var content = e.detail.value;
    this.setData({
      content: content
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
      wx.setNavigationBarTitle({
        title: '发表评价',
      })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})